#include <math/noise.h>
#include <cmath>

namespace math
{
Noise::Noise(uint64_t seed, int64_t baseX, int64_t baseZ, int64_t size) : 
    _size(size),
    _bitNoise1{seed & 0x00000000ffffffffu + 0x3A7B09EF00000000u},
    _bitNoise2{((seed & 0xffffffff00000000) >> 32U) + 0x000000009AA0BEF1u},
    _vertexValue{0}
{
    this->_vertexValue[0] = rand21(baseX, baseZ);
    this->_vertexValue[1] = rand21(baseX + size, baseZ);
    this->_vertexValue[2] = rand21(baseX, baseZ + size);
    this->_vertexValue[3] = rand21(baseX + size, baseZ + size);
}

//! @brief 随机函数 
uint64_t Noise::rand11(int64_t v)
{
    const uint64_t BIT_NOISE3 = 0x2DC490FA1B56C4E9u;
    uint64_t mangled = v;
    mangled *= this->_bitNoise1;
    mangled ^= (mangled >> 8);
    mangled += this->_bitNoise2;
    mangled ^= (mangled << 8);
    mangled *= BIT_NOISE3;
    mangled ^= (mangled >> 8);
    return mangled;
}

}